package com.lzb.musicplayer.utils;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.view.View;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.lzb.basic.utils.SPUtil;
import com.lzb.musicplayer.Content;

/**
 * 全局背景图片工具类
 * 用于管理应用的全局背景图片设置
 */
public class BackgroundUtil {

    /**
     * 设置Activity的背景图片
     * @param activity 目标Activity
     * @param imagePath 图片路径
     */
    public static void setActivityBackground(Activity activity, String imagePath) {
        if (activity == null || imagePath == null || imagePath.isEmpty()) {
            return;
        }

        try {
            // 获取根视图并设置背景
            View rootView = activity.getWindow().getDecorView().getRootView();
            if (rootView != null) {
                // 判断是Uri路径还是文件路径
                Object loadPath;
                if (imagePath.startsWith("content://")) {
                    loadPath = Uri.parse(imagePath);
                } else {
                    loadPath = imagePath;
                }
                
                Glide.with(activity)
                    .load(loadPath)
                    .into(new SimpleTarget<Drawable>() {
                        @Override
                        public void onResourceReady(Drawable resource, Transition<? super Drawable> transition) {
                            rootView.setBackground(resource);
                        }
                        
                        @Override
                        public void onLoadFailed(Drawable errorDrawable) {
                            // 图片加载失败时，使用默认背景颜色
                            setDefaultBackgroundColor(activity);
                        }
                    });
            }
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(activity, "背景图片加载失败: " + e.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 应用保存的背景图片到Activity
     * @param activity 目标Activity
     */
    public static void applySavedBackground(Activity activity) {
        if (activity == null) {
            return;
        }

        String savedBackgroundPath = SPUtil.getString(activity, Content.backgroundPath, "");
        if (!savedBackgroundPath.isEmpty()) {
            setActivityBackground(activity, savedBackgroundPath);
        } else {
            // 没有设置背景图片时，使用默认背景颜色
            setDefaultBackgroundColor(activity);
        }
    }

    /**
     * 清除背景图片
     * @param activity 目标Activity
     */
    public static void clearBackground(Activity activity) {
        if (activity == null) {
            return;
        }

        try {
            View rootView = activity.getWindow().getDecorView().getRootView();
            if (rootView != null) {
                rootView.setBackground(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 设置默认背景颜色
     * @param activity 目标Activity
     */
    public static void setDefaultBackgroundColor(Activity activity) {
        if (activity == null) {
            return;
        }

        try {
            View rootView = activity.getWindow().getDecorView().getRootView();
            if (rootView != null) {
                // 设置默认背景颜色 #0F0F0F
                rootView.setBackgroundColor(android.graphics.Color.parseColor("#0F0F0F"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 重置背景图片（清除保存的路径并设置为默认背景颜色）
     * @param context 上下文
     * @param activity 目标Activity
     */
    public static void resetBackground(Context context, Activity activity) {
        // 清除保存的路径
        SPUtil.putString(context, Content.backgroundPath, "");
        
        // 设置为默认背景颜色
        setDefaultBackgroundColor(activity);
    }
}